const req = require('../../req/index.js');

var pageData;
Page({
	data: {
		top: 0,
		errorMsg: '',
		successMsg: '',
		accordionActive: "",
		pageLoading: false,
		yyjlList: []
	},
	onLoad() {
		pageData = this.data;
		this.setData({
			// top: wx.getSystemInfoSync().statusBarHeight + 44,
			pageLoading : true
		})
		this.getAppionts();
		
	},
	goback() {
		wx.navigateBack();
	},
	getAppionts(){
		req.wfyy.getAppionts()
			.then((res) => {
				if (res.head.code == 1) {
					this.setData({
						yyjlList: res.dataList
					})
					console.log(this.data.yyjlList);
				} else {
					this.errorShow(res.head.message);
				}
			})
			.catch(req.err.show)
			.then(()=>{
				this.setData({
					pageLoading : false
				})
				// 结束下拉刷新
				wx.stopPullDownRefresh();
			});
	},
	//下拉刷新事件
	onPullDownRefresh: function () {
		this.refreshFunc();
	},
	// 下拉刷新时执行的函数
	refreshFunc: function () {
		this.getAppionts();
	},
	successShow(msg) {
		this.setData({
			successMsg: msg
		});
		this.selectComponent('#errorMsg').close();
		this.selectComponent('#successMsg').open();
	},
	errorShow(msg) {
		this.setData({
			errorMsg: msg
		});
		this.selectComponent('#successMsg').close();
		this.selectComponent('#errorMsg').open();
	},
	changeAccordion(e) {
		var accordionIndex = e.currentTarget.id;
		if (this.data.accordionActive == accordionIndex) {
			accordionIndex = '';
		}
		this.setData({
			accordionActive: accordionIndex
		});
	},
	cancelyy(e) {
		var that =this;
		wx.showModal({
			title: '提示',
			content: '确认取消预约当前业务?',
			confirmText: '确认',
			confirmColor: '#ff7373',
			success(res){
				if (res.confirm) {
					var id = e.currentTarget.id;
					that.setData({
						pageLoading: true
					});
					req.wfyy.cancelAppoint(id)
						.then((res) => {
							if (res.head.code == 1) {
								that.successShow(res.head.message);
								for(let i in pageData.yyjlList){
									if(pageData.yyjlList[i].id == id){
										pageData.yyjlList[i].zt = 'x';
										that.setData({
											yyjlList : pageData.yyjlList
										})
									}
								}
							} else {
								that.errorShow(res.head.message);
							}
						})
						.catch(req.err.show)
						.then(()=>{
							that.setData({
								pageLoading: false
							});
						});
				} else if (res.cancel) {

				}
			}
		})
	}
})
